/*
 * Copyright (C) 2022 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.xjh.him.adapter;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.view.View;
import android.widget.ImageView;

import androidx.core.content.ContextCompat;

import com.bumptech.glide.Glide;
import com.scwang.smartrefresh.layout.adapter.SmartRecyclerAdapter;
import com.scwang.smartrefresh.layout.adapter.SmartViewHolder;
import com.xjh.him.R;
import com.xjh.him.adapter.entity.Session;
import com.xjh.him.utils.Utils;
import com.xuexiang.xui.utils.DrawableUtils;
import com.xuexiang.xui.widget.textview.supertextview.SuperTextView;
import com.xuexiang.xutil.display.DensityUtils;
import com.yanzhenjie.recyclerview.OnItemClickListener;
import com.yanzhenjie.recyclerview.OnItemLongClickListener;

import org.xjh.tool.Checks;

import java.util.List;

public class SessionAdapter extends SmartRecyclerAdapter<Session> {
    private Activity mActivity;
    private List<Session> data;
    private OnItemClickListener itemClickListener;
    private OnItemLongClickListener itemLongClickListener;

    public SessionAdapter() {
        super(R.layout.adapter_session_item);
    }

    public SessionAdapter(Activity activity, List<Session> data) {
        super(data, R.layout.adapter_session_item);
        this.data = data;
        mActivity = activity;
    }

    @Override
    protected void onBindViewHolder(SmartViewHolder holder, Session session, int position) {
        SuperTextView stvSessionItem = holder.findViewById(R.id.stv_session_item);
        if (Checks.isEmpty(session.getGroupAvatar())) {
            BitmapDrawable bitmapDrawable = DrawableUtils.createCircleDrawableWithText(
                    mActivity.getResources(),
                    DensityUtils.dip2px(45),
                    ContextCompat.getColor(mActivity, R.color.app_color_theme_7),
                    "群",
                    com.xuexiang.xui.utils.DensityUtils.sp2px(mActivity, 20),
                    Color.WHITE);
            stvSessionItem.setLeftIcon(bitmapDrawable);
        }else {
            ImageView leftIconIV = stvSessionItem.getLeftIconIV();
            Utils.setImageViewParams(leftIconIV, 50, 50);
            Glide.with(mActivity)
                    .load(session.getGroupAvatar())
                    .apply(Utils.getRoundedCornersRequestOptions(5))
                    .into(leftIconIV);
        }
        stvSessionItem.setLeftTopString(session.getGroupName());
        stvSessionItem.setLeftBottomString(session.getNewestMsg());
        if (session.getIsTop()){
            stvSessionItem.setSelected(true);
        }else {
            stvSessionItem.setSelected(false);
        }

        stvSessionItem.setOnSuperTextViewClickListener(new SuperTextView.OnSuperTextViewClickListener() {
            @Override
            public void onClick(SuperTextView superTextView) {
                itemClickListener.onItemClick(mActivity.findViewById(R.id.session_list), position);
            }
        });
        stvSessionItem.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                itemLongClickListener.onItemLongClick(v, holder.getBindingAdapterPosition());
                return true;
            }
        });
    }

    public void setItemClickListener(OnItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public void setItemLongClickListener(OnItemLongClickListener itemLongClickListener) {
        this.itemLongClickListener = itemLongClickListener;
    }
}
